package com.skjanyou.applicationcase.util;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

public class RobotUtil{
	private static Robot robot;
	static{
		try {
			robot = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}
	/**截取屏幕上面一块区域**/
	public static BufferedImage createScreenCapture(Rectangle rec){
		BufferedImage image = robot.createScreenCapture(rec);
		return image;
	}
	/**截取屏幕上面一块区域,参数为左上角坐标，宽，高**/
	public static BufferedImage createScreenCapture(int x,int y,int width,int height){
		BufferedImage image = createScreenCapture(new Rectangle(x, y, width, height));
		return image;
	}
	
	/**
	 * 获取屏幕上某点的颜色
	 * 
	 * ***/
	public static String getPixelColor(int x, int y){
		String str = "";
		Color color = robot.getPixelColor(x, y);
		int red = color.getRed();
		int green = color.getGreen();
		int blue = color.getBlue();
		String hex = convertRGBToHex(red, green, blue);
		str = "red:" + red + ",green:" + green + ",blue:" + blue + ".Hex:" + hex;
		return str;
	}
	
	public RobotUtil keyClick(int keycode){
		robot.keyPress(keycode);
		robot.keyRelease(keycode);
		return this;
	}
	
	public RobotUtil keyPress(int keycode) {
		robot.keyPress(keycode);
		return this;
	}
	public RobotUtil keyRelease(int keycode) {
		robot.keyRelease(keycode);
		return this;
	}
	public RobotUtil mouseMove(int x, int y){
		robot.mouseMove(x, y);
		return this;
	} 
	public RobotUtil mousePress(int buttons){
		robot.mousePress(buttons);
		return this;
	}
	public RobotUtil mouseRelease(int buttons){
		robot.mouseRelease(buttons);
		return this;
	}
	public RobotUtil mouseWheel(int wheelAmt) {
		robot.mouseWheel(wheelAmt);
		return this;
	}
	
	private static String convertRGBToHex(int r, int g, int b) {    
	    String rFString, rSString, gFString, gSString, bFString, bSString, result;
	    int red, green, blue;
	    int rred, rgreen, rblue;
	    red = r / 16;
	    rred = r % 16;
	    if (red == 10) rFString = "A";
	    else if (red == 11) rFString = "B";
	    else if (red == 12) rFString = "C";
	    else if (red == 13) rFString = "D";
	    else if (red == 14) rFString = "E";
	    else if (red == 15) rFString = "F";
	    else rFString = String.valueOf(red);

	    if (rred == 10) rSString = "A";
	    else if (rred == 11) rSString = "B";
	    else if (rred == 12) rSString = "C";
	    else if (rred == 13) rSString = "D";
	    else if (rred == 14) rSString = "E";
	    else if (rred == 15) rSString = "F";
	    else rSString = String.valueOf(rred);

	    rFString = rFString + rSString;

	    green = g / 16;
	    rgreen = g % 16;

	    if (green == 10) gFString = "A";
	    else if (green == 11) gFString = "B";
	    else if (green == 12) gFString = "C";
	    else if (green == 13) gFString = "D";
	    else if (green == 14) gFString = "E";
	    else if (green == 15) gFString = "F";
	    else gFString = String.valueOf(green);

	    if (rgreen == 10) gSString = "A";
	    else if (rgreen == 11) gSString = "B";
	    else if (rgreen == 12) gSString = "C";
	    else if (rgreen == 13) gSString = "D";
	    else if (rgreen == 14) gSString = "E";
	    else if (rgreen == 15) gSString = "F";
	    else gSString = String.valueOf(rgreen);

	    gFString = gFString + gSString;

	    blue = b / 16;
	    rblue = b % 16;

	    if (blue == 10) bFString = "A";
	    else if (blue == 11) bFString = "B";
	    else if (blue == 12) bFString = "C";
	    else if (blue == 13) bFString = "D";
	    else if (blue == 14) bFString = "E";
	    else if (blue == 15) bFString = "F";
	    else bFString = String.valueOf(blue);

	    if (rblue == 10) bSString = "A";
	    else if (rblue == 11) bSString = "B";
	    else if (rblue == 12) bSString = "C";
	    else if (rblue == 13) bSString = "D";
	    else if (rblue == 14) bSString = "E";
	    else if (rblue == 15) bSString = "F";
	    else bSString = String.valueOf(rblue);
	    bFString = bFString + bSString;
	    result = "#" + rFString + gFString + bFString;
	    return result;
	}
	
//	public static void main(String[] args) {
//		BufferedImage img = robot.createScreenCapture(new Rectangle(0, 0, 1366, 768));
//		ImageUtil.saveImage(img, "c:/", "捕获屏幕.jpg");
//		getPixelColor(999,750);
//	}

}
